﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbUpgrade.General.更新数据库
{
    /// <summary>
    /// 升级数据库基类
    /// </summary>
    public abstract class UpGradeDbBase
    {
        #region 版本
        /// <summary>
        /// 版本
        /// </summary>
        protected int version = 1;
        /// <summary>
        /// 版本
        /// </summary>
        public int Version { get { return version; } }
        #endregion

        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public UpGradeDbBase()
        {
            SetVersion();
        }
        #endregion

        #region 抽象方法

        #region 设置版本
        /// <summary>
        /// 设置版本
        /// </summary>
        protected abstract void SetVersion();
        #endregion

        #region 执行更新语句
        /// <summary>
        /// 执行更新语句
        /// </summary>
        protected abstract void ExecSql();
        #endregion

        #endregion

        #region 内部方法

        #region 删除表
        /// <summary>
        /// 删除表
        /// </summary>
        /// <param name="tableName"></param>
        private void DropTable(string tableName)
        {
            string sql = string.Format("select count(*) tCount from sqlite_master where type='table' and name='{0}'", tableName);
            int vcount = App.Dal.ExecScalar(sql).ToInt32Ex();
            if (vcount > 0)
            {
                sql = string.Format("DROP TABLE '{0}'", tableName);
                App.Dal.ExecNoQuery(sql);
            }

        }
        #endregion

        #region 更新版本
        /// <summary>
        /// 更新版本
        /// </summary>
        private void UpdateVersion()
        {
            string sql = "select count(*) from SqliteVersion where Id = 1";
            int icount = App.Dal.ExecScalar(sql).ToInt32Ex();
            if (icount <= 0)
            {
                sql = string.Format("insert into SqliteVersion (Id,Version) values (1,{0})", version);
            }
            else
            {
                sql = string.Format("UPDATE SqliteVersion set Version = {0} where Id = 1", version);
            }
            App.Dal.ExecNoQuery(sql);
        }
        #endregion

        #endregion

        #region 子类可用方法

        #region 创建表
        /// <summary>
        /// 创建表
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="sql"></param>
        protected void CreateTable(string tableName, string sql)
        {
            DropTable(tableName);
            App.Dal.ExecNoQuery(sql);
        }
        #endregion

        #endregion

        #region 公共方法

        #region 获取版本
        /// <summary>
        /// 获取版本
        /// </summary>
        /// <returns></returns>
        public int GetVersion()
        {
            return version;
        }
        #endregion

        #region 更新数据库版本
        /// <summary>
        /// 更新数据库版本
        /// </summary>
        public void UpGradeVersion()
        {
            ExecSql();
            UpdateVersion();
        }
        #endregion

        #endregion
    }
}
